ExternalPaperModel.controller('externalResultModal', ['$scope', 'externalResultFactory', '$rootScope', 'sysUserInfo', 'RequestTrainService', 'Comm', 'MsgCon', 'Msg', '$sce', '$stateParams', '$state', function ($scope, externalResultFactory, $rootScope, sysUserInfo, RTS, Comm, MsgCon, Msg, $sce, $stateParams, $state) {

    //关闭窗口
    $scope.closeWindow = function () {
        externalResultFactory.deactivate();
    }
    // 选中个数
    $scope.checkAll = 0;
    //全选
    $scope.selectAll = function () {
        $scope.selFlag = true;
        if ($scope.checkAll == $scope.List.length) { //已选
            $scope.selFlag = false;
            $scope.checkAll = 0;
        } else {
            $scope.selFlag = true;
            $scope.checkAll = $scope.List.length;
        }
        // 设置样式
        angular.forEach($scope.List, function (data, index, array) {
            data.isSel = $scope.selFlag;
        });
    }
    // 单选
    $scope.singleCheck = function (item) {
        if (item.isSel) {
            $scope.checkAll--;
            item.isSel = false;
        } else {
            $scope.checkAll++;
            item.isSel = true;
        }
    }
    /****************************************************人员选择器************************************************/
    //人员选择器的定位
    $scope.mousePosition = function (ev) {
        if (!ev) {
            ev = window.event;
        }

        if (ev.type != "click") {
            return;
        }
        var width = 0;
        var height = 0;
        if (ev.pageX || ev.pageY) {//firefox、chrome等浏览器
            if (document.documentElement.clientWidth - ev.pageX < 450) {
                width = ev.pageX - (450 - (document.documentElement.clientWidth - ev.pageX)) - 20;
            } else {
                width = ev.pageX;
            }
            if (document.documentElement.clientHeight - ev.pageY < 480) {
                height = ev.pageY - (480 - (document.documentElement.clientHeight - ev.pageY)) - 20;
            } else {
                height = ev.pageY;
            }

            return { "left": width + "px", "top": height + "px", "position": "fixed" };
        }

        return {// IE浏览器
            "left": ev.clientX + document.body.scrollLeft - document.body.clientLeft,
            "top": ev.clientY + document.body.scrollTop - document.body.clientTop
        };
    }

    // 人员选择器                
    $scope.personData = {
        menu: [//进入人员选择器的菜单
        {"id": "User"}],
        ApiOpen: false,
        windowModal: 1,
        userMore: false, //不要全选，和已选
        style: "",
        ViceuserOrgList: [],
        ViceuserRoleList: [],
        ViceuserGroupList: [],
        searchType: [],  //初始化教师查询
        DB: "0",
        DBdata: [],
        UserList: []
    }

    //打开人员悬着器
    $scope.openPerson = function (event) {
        $scope.personData.style = $scope.mousePosition(event);
        $scope.personData.ApiOpen = true;
    }
    //关闭事件
    $scope.$watch('personData.UserList', function (newValue, oldValue, $scope) {
        if ($scope.personData.UserList.length > 0) {
            $scope.addInfo.userId = $scope.personData.UserList[0].user_ID;
            $scope.addInfo.userimg = $scope.personData.UserList[0].user_Img;
            $scope.addInfo.username = $scope.personData.UserList[0].user_Name;
        } else {
            $scope.addInfo.userId = "";
            $scope.addInfo.userimg = "";
            $scope.addInfo.username = "";
        }
        $scope.personData.ApiOpen = false;
    }, true);
    $scope.Loading = false;
    //初始化参数
    $scope.params = {
        searchText: "",
        userId: sysUserInfo.getuserinfo().user_ID,
        externalId: $scope.resultid,
        orgid: sysUserInfo.getuserinfo().organization_ID,
        pageIndex: 1,
        pageSize: 10
    };

    /*分页*/
    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 0,
        itemsPerPage: 10,
        perPageOptions: [10, 20],
        onChange: function () {
            if ((parseInt($scope.params.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.params.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.params.pageIndex = $scope.paginationConf.currentPage;
                $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.GetList();
            }
        }
    };
    $scope.List = [];
    //查询试卷列表
    $scope.GetList = function () {
        if ($scope.Loading) {
            Msg.warning("正在查询，请稍后!");
            return;
        }
        $scope.Loading = true;
        RTS.findScorePage($scope.params).then(function (data) {
            $scope.Loading = false;
            if (data.errorcode == "0") {
                $scope.List = data.datas;
                if ($scope.List == undefined || $scope.List.length <= 0) {
                    return;
                } else {
                    $scope.paginationConf.totalItems = data.numCount;
                    $scope.paginationConf.currentPage = data.pageIndex;
                }
            } else {
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.Loading = false;
        });
    }

    /************************************************新增编辑成绩*******************************************/
    //新增弹框
    $scope.showAdd = false;
    $scope.addInfo = {};
    //初始新增参数
    $scope.initAdd = function () {
        $scope.addInfo = {
            externalScoreId: "", //主键id
            userId: "",
            userimg: "",
            username: "",
            score: "",
            externalId: $scope.resultid,
            createUserId: sysUserInfo.getuserinfo().user_ID,
            pass: false//是否通过
        };
    }
    //添加成绩
    $scope.AddScore = function () {
        if (!$scope.showAdd) {
            $scope.initAdd();
        }
        $scope.showAdd = !$scope.showAdd;
    }

    $scope.isbtnload = false;
    //保存
    $scope.saveScore = function () {
        if ($scope.addInfo.userId.length <= 0) {
            Msg.warning("请选择用户！");
            return;
        }
        if ($scope.addInfo.score <= 0) {
            Msg.warning("请输入正确的成绩！");
            return;
        }
        $scope.isbtnload = true;
        RTS.saveOrEditScore($scope.addInfo).then(function (data) {
            $scope.isbtnload = false;
            if (data.errorcode == "0") {
                Msg.success("添加成功！");
                $scope.checkAll = 0; // 复选框选中的数量            
                $scope.getList();  // 重新请求一下数据
            } else {
                Msg.error("请求数据有误");
            }
        }, function callback() {
            Msg.error("请求数据有误");
            $scope.isbtnload = false;
        });
    }
    // 删除
    $scope.delinfo = function (item) {

        var ids = [];
        $scope.List.forEach(function (data, index) {
            if (data.isSel) {
                ids.push(data.externalScoreId);
            }
        });
        // 未选中
        if (Comm.isNull(ids) || ids.length <= 0) {
            Msg.warning("提示", "请选择要删除的数据!");
            return;
        }
        //提示框
        MsgCon.delnoicon().then(function (flag) {   // 删除弹框
            if (flag) {
                // 发送请求
                RTS.delScore({ userid: sysUserInfo.getuserinfo().user_ID, externalScoreId: ids.join(","), externalId: $scope.resultid }).then(function (respose) {
                    if (respose.errorcode == "0") {
                        Msg.success("删除成功！");
                        $scope.checkAll = 0; // 复选框选中的数量            
                        $scope.getList();  // 重新请求一下数据
                    } else {
                        Msg.error("操作失败！");
                    }
                }, function errorCallback() {
                    console.log("连接服务器失败！");
                });
            }
        }, function (p) { console.log('error') });
    }

} ]);
